#include <Windows.h>
#include "../Header Files/CGame.h"
#include "../../Renderer/Header Files/Direct3D_AL.h"
#include "../../Misc/CDirectInput.h"


const char * g_szWINDOW_CLASS_NAME	= "WindowClass";

const char * g_szWINDOW_TITLE		= "Placeholder";

Direct3D_AL	*D3D = NULL;

#if _DEBUG
const bool	g_bIS_WINDOWED			= true;
const int	g_nWINDOW_WIDTH			= (int)1024;
const int	g_nWINDOW_HEIGHT		= (int)768;
#pragma comment(linker, "/SUBSYSTEM:Console") 
#else
const BOOL	g_bIS_WINDOWED			= TRUE;
const int	g_nWINDOW_WIDTH			= (int)GetSystemMetrics(SM_CXFULLSCREEN);
const int	g_nWINDOW_HEIGHT		= (int)GetSystemMetrics(SM_CYFULLSCREEN);
#pragma comment(linker, "/SUBSYSTEM:Windows") 
#endif
#include "../../Misc/CTimer.h"

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT	ps;
	HDC			hdc;

	switch(uiMsg)
	{
	case WM_SYSKEYUP:
	case WM_SYSCHAR:
		return(0);
		break;

	case WM_CLOSE:
		{
			DestroyWindow(hWnd);
		}
		break;

	case WM_ACTIVATE:
		{
			if (LOWORD(wParam) != WA_INACTIVE)
			{
			}
			else
			{
			}
		}
		break;

	case WM_CREATE: 
		{
			return(0);
		}
		break;

	case WM_PAINT:
		{
			hdc = BeginPaint(hWnd,&ps);
			EndPaint(hWnd,&ps);
			return(0);
		}
		break;

	case WM_DESTROY: 
		{
			PostQuitMessage(0);
			DestroyWindow(hWnd);
			return(0);
		}
		break;

	default:
		break;
	}

	return (DefWindowProc(hWnd, uiMsg, wParam, lParam));
}

BOOL CheckIfAlreadyRunning(void)
{
	HWND hWnd = FindWindow(g_szWINDOW_CLASS_NAME, g_szWINDOW_TITLE);

	if (hWnd)
	{
		if (IsIconic(hWnd))
				ShowWindow(hWnd, SW_RESTORE);

		SetForegroundWindow(hWnd);

		return TRUE;
	}

	return FALSE;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	MSG		msg;

	D3D = Direct3D_AL::GetInstance();
	D3D->Init(hInstance, g_nWINDOW_WIDTH, g_nWINDOW_HEIGHT);
	if (!hPrevInstance)
		if (CheckIfAlreadyRunning())
			return FALSE;

	if (!D3D->InitializeWindow((WNDPROC)WindowProc))
		return 0;

	if (!D3D->m_pHWND)
		return 0;
	
	D3D->InitializeDirectX();

	//ShowCursor(false);
	CDirectInput::GetInstance()->InitDirectInput(D3D->m_pHWND, D3D->m_pHInstance, DI_KEYBOARD | DI_MOUSE, 0);
	CGame* m_pGame = CGame::GetInstance();

	m_pGame->Initialize();
	CTimer* pTime = new CTimer();
	pTime->Reset();
	DEBUG_OUTPUT("Debug Window:");
	while (TRUE)
	{
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{ 
			if (msg.message == WM_QUIT)
				break;

			TranslateMessage(&msg);

			DispatchMessage(&msg);
		}
		float time = pTime->GetDeltaTime();
		pTime->Update();
		if(m_pGame->Update(pTime->GetDeltaTime()) == false)
			PostQuitMessage(0);
	}
	delete pTime;
	m_pGame->Shutdown();
	delete m_pGame;

	UnregisterClass(g_szWINDOW_CLASS_NAME, hInstance);

	ShowCursor(true);
	
	return (int)(msg.wParam);
}

int main(int nArgc, char* nArgv[])
{
	WinMain(GetModuleHandle(NULL), NULL, GetCommandLine(), SW_SHOWDEFAULT);
	return 0;
}
